﻿--[[
   @brief    FriendBannerList
   @author   vincentlao
   @version  1.0
   @date     2015-06-11
--]]

ierd_tgp.tpf_ui.Log("loading banner_list.lua");

ierd_tgp.tpf_ui.Log("Require model.lua");
ierd_tgp.tpf_ui.Require("social\\friend_list\\model.lua");

ierd_tgp.tpf_ui.Log("Require friend_banner.lua");
-- ierd_tgp.tpf_ui.Require("social\\friend_list\\friend_banner.lua");
ierd_tgp.tpf_ui.Require("social\\talk_dialog\\control.lua");
FriendBannerList = {
    list_ctr_ = nil;
    uilib_ = nil;
    in_refreshing = false;
    friend2banner_ = {};
    ctrl2banner_ = {};
    lasttime_ = os.time();
};

function FriendBannerList:log(msg, core_msg)
    base_utility.log(string.format("[FriendBannerList] %s", msg), core_msg);
end

function FriendBannerList:attach(uilib, ctrl_id)
    self:log(string.format("attach %d", ctrl_id));
    self.uilib_ = ui_utility.uilib();
    self.list_ctr_ = MainCommon.get_ctrl_ptr(ui_utility.uilib(), ctrl_id, "Tenio::IDraggableList");
    FriendBannerListEventListener.add_tick();
end

function FriendBannerList:detach()
    self:log("detach");
    self.friend2banner_ = {};
    self.ctrl2banner_ = {};
    self.list_ctr_ = nil;
    FriendBannerListEventListener.remove_tick();
end

function FriendBannerList:show(show)
    if self.list_ctr_ ~= nil then
        self.list_ctr_:Show(show);
    end    
end

function FriendBannerList:show_friend_list()
    --[[self:remove_all_friend();]]--
    friend_list_model.sort();
    --[[local friends = friend_list_model.get_friend_list();
    if friends == nil then
        return;
    end

    for i = 1, #friends do
        self:insert_friend(i - 1, friends[i]);
    end]]--
end

function FriendBannerList:re_sort_friend()
    friend_list_model.sort();
    
    if self.list_ctr_ == nil then
        self:log(string.format("insert_friend, list_ctr_ is NIL, may be not attached yet"), true);
        return;
    end

    local friends = friend_list_model.get_friend_list();
    if friends == nil then
        return;
    end

    self.friend2banner_ = {};

    for i = 1, #friends do
        local item_id = self.list_ctr_:GetItemRootIDByIdx(i - 1);
        if item_id == nil or item_id == 0 then
            self:log(string.format("re_sort_friend, GetItemRootIDByIdx failed"));
            break;
        end

        local banner = self.ctrl2banner_[item_id];
        if banner == nil then
            self:log(string.format("re_sort_friend, get banner failed"));
            break;
        end

        banner.friend_id_ = friends[i].friend_id;
        self.ctrl2banner_[item_id] = banner;
        self.friend2banner_[friends[i].friend_id] = banner;
        banner:update(friends[i].friend_id);
    end
end

function FriendBannerList:insert_friend(pos, friend_info)
    self:log(string.format("insert_friend"));
    if self.list_ctr_ == nil then
        self:log(string.format("insert_friend, list_ctr_ is NIL, may be not attached yet"), true);
        return;
    end

    if self.in_refreshing then
        self:log(string.format("[insert_friend]return directtly while in refreshing"));
        return;
    end

    local const_ui_str, str_hoder = U8toCUIStr("social\\friend_banner.tpf");
    local item_id = self.list_ctr_:InsertItem(pos, const_ui_str);
    self.list_ctr_:SetItemDraggable(pos, false);
    self:re_arrange_item_pos();

    local banner = FriendBanner:new(friend_info.friend_id, item_id);
    self.ctrl2banner_[item_id] = banner;
    self.friend2banner_[friend_info.friend_id] = banner;
end

function FriendBannerList:on_delete_friend(friend_id)
end;

function FriendBannerList:on_friend_new_message_come(msg)
    if msg == nil or msg.friend_id == nil then
        return;
    end
    -- friend_list_model.set_last_msg(msg.friend_id, msg); 最后一条消息，让新IM消息模块管理

    local social_network =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    social_network:set_friend_last_msg(base_utility.table_to_json(msg));  

    local is_session_opened = talk_dialog_control.is_friend_session_active(msg.friend_id);
    if is_session_opened == false then
        self:re_sort_friend();
    end
end

function FriendBannerList:on_send_msg_to_friend(msg)
    if msg == nil or msg.friend_id == nil then
        return;
    end

    friend_list_model.set_last_msg(msg.receiver, msg);
    self:re_sort_friend(); 

    local social_network =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    social_network:set_friend_last_msg(base_utility.table_to_json(msg));      
end;

function FriendBannerList:on_get_friendlist_unread_msg_count(unread_msg)
    if unread_msg == nil then
        return;
    end
    
    local show_red_poind = false;

    for friend_i = 1, #unread_msg do
        --friend_message_model.clear_friend_messages(unread_msg[friend_i].id);
        --if friend_list_model.get_history_unread_msg_count(unread_msg[friend_i].id) == 0 then
            friend_list_model.set_history_unread_msg_count(unread_msg[friend_i].id, unread_msg[friend_i].count);
            friend_list_model.set_history_unread_msg_count_remotely(unread_msg[friend_i].id, unread_msg[friend_i].count);        
        --end
        
        if friend_list_model.get_history_unread_msg_count(unread_msg[friend_i].id) > 0 then
            local unread_message_event = {};
            unread_message_event.friend_id = unread_msg[friend_i].id;  
            event_center.send_event(group_event.FRIEND_HAVE_UNREAD_MESSAGE,base_utility.table_to_json(unread_message_event));        
        end

        if friend_list_model.get_history_unread_msg_count(unread_msg[friend_i].id) > 0 then
            show_red_poind = true;
        end
    end

    --friend_message_model.clear_all_friend_messages();

    self:re_sort_friend(); 

    --[[local show_red_point = {};
    show_red_point.show = "true";  

    if show_red_poind == true then
        show_red_point.show = "true";  
    else
        show_red_point.show = "false";  
    end

    event_center.send_event(group_event.SHOW_FRIEND_TAB_RED_POINT,base_utility.table_to_json(show_red_point)); ]]--
    local count_friend = friend_list_model.get_total_unread_msg_count();
    local count_group = group_list_model.get_total_unread_msg_count();
    local total = count_friend + count_group;

    local show_red_point = {};
    if total == 0 then
        show_red_point.show = "false";
    else
        show_red_point.show = "true";
    end
    event_center.send_event(group_event.SHOW_FRIEND_TAB_RED_POINT, base_utility.table_to_json(show_red_point));
end;   

function FriendBannerList:on_friend_clear_message(friend_id)
    self:re_sort_friend();
    self:refresh_red_point();
end

function FriendBannerList:refresh_red_point()
    --[[local show_red_poind = false;

    for k,v in pairs(friend_message_model.friend_message_) do
        if #v > 0 then
            show_red_poind = true;
        end
    end

    for index = 1, #friend_list_model.friend_table_ do
        if friend_list_model.get_history_unread_msg_count(friend_list_model.friend_table_[index].friend_id) > 0 then
            show_red_poind = true;
        end
    end

    local show_red_point = {};
    show_red_point.show = "true";  

    if show_red_poind == true then
        show_red_point.show = "true";  
    else
        show_red_point.show = "false";  
    end

    event_center.send_event(group_event.SHOW_FRIEND_TAB_RED_POINT,base_utility.table_to_json(show_red_point)); ]]--
    local count_friend = friend_list_model.get_total_unread_msg_count();
    local count_group = group_list_model.get_total_unread_msg_count();
    local total = count_friend + count_group;

    local show_red_point = {};
    if total == 0 then
        show_red_point.show = "false";
    else
        show_red_point.show = "true";
    end
    event_center.send_event(group_event.SHOW_FRIEND_TAB_RED_POINT, base_utility.table_to_json(show_red_point));
end

function FriendBannerList:update_friend_online_status(friend_id, gameid, status, sub_status)
    friend_list_model.set_friend_online_status(friend_id, gameid, status, sub_status);
    self:re_sort_friend();
end

function FriendBannerList:remove_friend(friend_id)
    self:log(string.format("remove_group"));
    if self.list_ctr_ == nil then
        self:log(string.format("remove_group, list_ctr_ is NIL, may be not attached yet"), true);
        return;
    end

    if self.in_refreshing then
        self:log(string.format("[remove_group]return directtly while in refreshing"));
        return;
    end

    local banner = FriendBannerList:get_banner(friend_id);
    if banner == nil then
        return;
    end

    local item_id = banner:get_root_id();
    local item_index = self.list_ctr_:GetIdxByItemRootID(item_id);
    self.list_ctr_:RemoveItem(item_index);
    self:re_arrange_item_pos();

    self.friend2banner_[friend_id] = nil;
    self.ctrl2banner_[item_id] = nil;

    friend_list_model.remove_friend_info_by_id(friend_id);
    friend_message_model.clear_friend_messages(friend_id);

    self:refresh_red_point();

    local param = {};
    param["friend_id"] = friend_id;
    event_center.send_event(group_event.REMOVE_FRIEND_FROM_FRIEND_LIST,base_utility.table_to_json(param));
end

function FriendBannerList:remove_all_friend()
    self:log(string.format("remove_all_friend"));
    if self.list_ctr_ == nil then
        self:log(string.format("remove_all_friend, list_ctr_ is NIL, may be not attached yet"), true);
        return;
    end

    if self.in_refreshing then
        self:log(string.format("[remove_all_friend]return directtly while in refreshing"));
        return;
    end

    while self.list_ctr_:GetItemCount() > 0 do
        self.list_ctr_:RemoveItem(0);
    end

    self:re_arrange_item_pos();

    self.friend2banner_ = {};
    self.ctrl2banner_ = {};

    friend_message_model.clear_all_friend_messages();
    self:refresh_red_point();
end

function FriendBannerList:check_friend_by_banner_id(banner_id)
    for key, value in pairs(self.ctrl2banner_) do
        if key == banner_id then
            value:set_selected(true);
            value:set_selected_state();
        else
            value:set_selected(false);
            value:set_normal_state();
        end
    end
end

function FriendBannerList:re_arrange_item_pos()
    self:log(string.format("re_arrange_item_pos"));
    if self.list_ctr_ == nil then
        self:log(string.format("re_arrange_item_pos, list_ctr_ is NIL, may be not attached yet"),
                            true);
        return;
    end
    
    if self.in_refreshing then
        self:log(string.format("[re_arrange_item_pos]return directtly while in refreshing"));
        return;
    end
    
    self.list_ctr_:ReArrangeItemPos();
end

function FriendBannerList:refresh_list()
    if self.list_ctr_ == nil then
        self:log(string.format("refresh_list, list_ctr_ is NIL, may be not attached yet"));
        return;
    end
    self.friend2banner_ = {};
    self.ctrl2banner_ = {};

    -- 设置列表控件的Banner数量
    --self.list_ctr_:SetItemCount(count);
    -- 遍历game_list， 逐一设置数据

    self.in_refreshing = false;
    self:re_arrange_item_pos();
end

function FriendBannerList:get_banner(friend_id)
    self:log(string.format("get_banner, friend_id=%s", friend_id));
    return self.friend2banner_[friend_id];
end

-- 根据Banner控件id取banner对象
function FriendBannerList:get_banner_by_ctrl(id)
    self:log(string.format("get_banner_by_ctrl, banner_id=%d", id));
    local wrapper_id = MainCommon.get_parent_id(self.uilib_, id);
    return self:get_banner_by_wrapper(wrapper_id);
end

-- 根据Banner控件id取banner对象
function FriendBannerList:get_banner_by_wrapper(id)
    self:log(string.format("get_banner_by_wrapper, wrapper_id=%d", id));
    return self.ctrl2banner_[id];
end

function FriendBannerList:on_tick()
    local lastdate = os.date("*t", self.lasttime_);

    local currenttime = os.time();
    local currentdate = os.date("*t", currenttime);
    self.lasttime_ = currenttime;

    if currentdate.day > lastdate.day then
        self:re_sort_friend();
    end
end

function FriendBannerList:set_draggable(index, is_draggable)
    if self.list_ctr_ then
        self.list_ctr_:SetItemDraggable(index, is_draggable);
    end
end

function FriendBannerList:on_lol_game_staus_changed(data)
    if type(data) ~= "table" then
        return;
    end

    for i, friend_id in ipairs(data) do        
        local banner = FriendBannerList:get_banner(friend_id);
        if banner ~= nil then
            banner:update_lol_status();
        end
    end
end

---------------------------------------------------------------------------------------------------
-- 
---------------------------------------------------------------------------------------------------
FriendBannerListEventListener = {
    initialed_ = false;
};

function FriendBannerListEventListener.init()
    if initialed_ then
        return;
    end;
    
end;

function FriendBannerListEventListener.add_tick()
    base_utility.log(string.format("[BannerListEventListener]add_tick"));
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:AddInterval("FriendBannerListEventListener.on_tick", 30 * 60 * 1000);
end;

function FriendBannerListEventListener.remove_tick()
    base_utility.log(string.format("[BannerListEventListener]remove_tick"));
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval("FriendBannerListEventListener.on_tick");  
end;

function FriendBannerListEventListener.on_tick()
    FriendBannerList:on_tick();
end;

---------------------------------------------------------------------------------------------------
-- 
---------------------------------------------------------------------------------------------------
FriendBannerListUIListener = {
};
